package com.crl.rcdp.sys.controller;

import com.cars.util.global.GlobalReturnCode;
import com.cars.util.json.JsonResult;
import com.cars.util.json.JsonResultUtil;
import com.cars.util.validation.DeleteValidGroup;
import com.crl.rcdp.sys.dto.SysApiDTO;
import com.crl.rcdp.sys.service.SysApiService;
import com.crl.rcdp.sys.vo.SysApiSearchVO;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

/**
 * @description:
 * @author: 孔垂云
 * @version: 1.0
 * @date: 2020/6/22 17:43
 */
@RestController
@RequestMapping("/sys/api")
@Api(tags = "接口设置")
public class SysApiController {

    @Autowired
    private SysApiService sysApiService;

    /**
     * 获取所有接口
     */
    @PostMapping("/list")
    @ApiOperation(value = "接口列表")
    public JsonResult list(@RequestBody SysApiSearchVO sysApiSearchVO) {
        return JsonResultUtil.ok(sysApiService.page(sysApiSearchVO));
    }


    /**
     * 新增接口
     *
     * @return
     */
    @PostMapping("/add")
    @ApiOperation(value = "新增接口")
    public JsonResult add(@RequestBody SysApiDTO sysApiDTO) {
        int flag = sysApiService.add(sysApiDTO);
        if (flag == 1) {
            return JsonResultUtil.ok();
        } else {
            return JsonResultUtil.error(GlobalReturnCode.SERVER_ERROR);
        }
    }

    /**
     * 新增接口
     *
     * @return
     */
    @PostMapping("/update")
    @ApiOperation(value = "修改接口")
    public JsonResult update(@RequestBody SysApiDTO sysApiDTO) {
        int flag = sysApiService.update(sysApiDTO);
        if (flag == 1) {
            return JsonResultUtil.ok();
        } else {
            return JsonResultUtil.error(GlobalReturnCode.SERVER_ERROR);
        }
    }

    /**
     * 删除接口
     *
     * @return
     */
    @PostMapping("/delete")
    @ApiOperation(value = "删除接口")
    public JsonResult delete(@Validated({DeleteValidGroup.class}) @RequestBody SysApiDTO sysApiDTO) {
        int flag = sysApiService.delete(sysApiDTO.getId());
        if (flag == 1) {
            return JsonResultUtil.ok();
        } else {
            return JsonResultUtil.error(GlobalReturnCode.SERVER_ERROR);
        }
    }


    /**
     * 获取所有接口信息
     */
    @PostMapping("/get")
    @ApiOperation(value = "获取单个用户")
    public JsonResult get(@RequestBody SysApiSearchVO sysApiSearchVO) {
        SysApiDTO sysApiDTO = sysApiService.get(sysApiSearchVO.getId());
        return JsonResultUtil.ok(sysApiDTO);
    }


}
